/**
 * @Title: RecordAdapter.java
 * @Package: com.jason.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月19日23:43:26
 * @Version: V1.0
 */
package com.jason.fingerprint.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.jason.fingerprint.R;
import com.jason.fingerprint.beans.ui.ImageOrAudioBean;
import com.jason.fingerprint.ui.RecorderManagerActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: RecorderAdapter
 * @Description: RecorderAdapter 录音管理 ListView 适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015年10月20日16:54:37
 */
public class RecorderAdapter extends BaseAdapter {

	private static final String TAG = RecorderAdapter.class.getSimpleName();

	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private List<ImageOrAudioBean> imageOrAudioBean;

	public RecorderAdapter(){
		this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.imageOrAudioBean = new ArrayList<ImageOrAudioBean>();
	}

	public RecorderAdapter(Context context){
		this.mContext = context;
		this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.imageOrAudioBean = new ArrayList<ImageOrAudioBean>();
	}

	public RecorderAdapter(Context context, List<ImageOrAudioBean> imageOrAudioBean){
		this.mContext = context;
		this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.imageOrAudioBean = imageOrAudioBean;
	}

	@Override
	public int getCount() {
		return isEmptyOfRecordBeans() ? imageOrAudioBean.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return isEmptyOfRecordBeans() ? imageOrAudioBean.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.item_recorder_listview, null);
			holder = new Holder();
			holder.mNameTextView = (TextView) convertView.findViewById(R.id.name);
			holder.mDeleteTextView = (TextView) convertView.findViewById(R.id.delete);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		final ImageOrAudioBean bean = (ImageOrAudioBean)getItem(position);
		if (bean != null) {
			holder.mNameTextView.setText(bean.getFileName());
			holder.mDeleteTextView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					((RecorderManagerActivity)mContext).deleteRecorder(bean);
				}
			});
		}
		return convertView;
	}
	
	class Holder{
		TextView mNameTextView;
		TextView mDeleteTextView;
	}
	
	//判断当前数据是否为空
	public boolean isEmptyOfRecordBeans(){
		return imageOrAudioBean != null && !imageOrAudioBean.isEmpty() && imageOrAudioBean.size() > 0;
	}

}
